<?php
namespace app\shop\model;
use think\Validate;
use think\Db;
use app\base\model\Base;
class GoodCatM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'name' => 'require',
               'is_show' => 'require|number',
               'parent_id' => 'require|number',
               'sort_num' => 'require|number',
           ];
        $msg = [
        'name.require' => '分类名称不能为空',
        'sort_num.require' => '排序不能为空',
        'sort_num.number' => '排序只能是数字',
        'parent_id.require' => '上级分类不能为空',
        'parent_id.number' => '上级分类只能是数字',
        'is_show.require' => '是否菜单显示不能为空',
        'is_show.number' => '是否菜单显示只能是数字',
        ];
           $validate = new Validate($rule,$msg);
           $validate->scene('add', ['name','sort_num','parent_id','is_show']);
           $validate->scene('edit', ['name','sort_num','parent_id','is_show']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {

        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $admin_id = session('admin_id');
        $insertData = [];
        //处理图片
        $ret = uploadOne('ico', array(array(100, 100)));
        if($ret['ok'] == 1)
        {
            $insertData['ico'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
            $insertData['ico'] = '';
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }


        $insertData['admin_id'] = $admin_id;
        $insertData['name'] = $data['name'];
        $insertData['parent_id'] = $data['parent_id'];
        $insertData['is_show'] = $data['is_show'];
        $insertData['sort_num'] = $data['sort_num'];
        $res = Db::table('shop_goods_cat')->insertGetId($insertData);
        if(!$res)
        {
            $this->code = -1;
            $this->msg = '新增失败!';
            return false;
        }
        return true;
    }

    /**
     * 全部数据
     */
    public function lst()
    {
        $data = $this->getCatTree();
        return $data;
    }

    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $admin_id = session('admin_id');
        $insertData = [];
        //处理图片
        $ret = uploadOne('ico', array(array(100, 100)));
        if($ret['ok'] == 1)
        {
            $insertData['ico'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }


        $insertData['admin_id'] = $admin_id;
        $insertData['name'] = $data['name'];
        $insertData['parent_id'] = $data['parent_id'];
        $insertData['is_show'] = $data['is_show'];
        $insertData['sort_num'] = $data['sort_num'];
        $res = Db::table('shop_goods_cat')->where('id',$data['id'])->update($insertData);
        if(!$res)
        {
            $this->code = -1;
            $this->msg = '修改失败!';
            return false;
        }
        return true;
    }



    /**********************************获取级别分类和其子分类分类展示到前台导航************************/
    /*
     * @param $one 级别level
     * @param $child true为要子分类 false为不要子分类
     */
    public function getLevelCat($one=1,$child=true){
        //判断是否有数据缓存
        $info=$this->getCatTree();
        $data=array();
        foreach($info as $k=>$v){
            if($v['level']==$one){
                if($child){
                    foreach($info as $k1=>$v1){
                        if($v1['parent_id']==$v['id']){
                            $v['child'][]=$v1;
                        }
                    }
                }
                $data[]=$v;
            }
        }
        //把数据缓存起来
        return $data;

    }
    /***********************************获得一个分类的级别**********************************************/
    /*
     * @param $cid 分类ID
     */
    public function getLevelByCid($cid){
        $info=$this->getCatTree();
        foreach($info as $k=>$v){
            if($v['id']==$cid)
                return $v['level'];
        }
    }
    /******************************************获取上级分类********************************************/
    public function parentCat($catid){
        static $data=array();
        $info=Db::table('shop_goods_cat')->field('id,name,parent_id')->find($catid);
        $data[]=$info;
        if($info['parent_id']>0)
            $this->parentCat($info['parent_id']);
        return array_reverse($data);
    }


    /***********************************获取服务分类树*************************************************/
    public function getCatTree(){
        $info=Db::table('shop_goods_cat')->select();
        $data=$this->_getCatTree($info,$parent_id=0,$level=0,TRUE);
        return $data;
    }
    private function _getCatTree($info,$parent_id=0,$level=0,$isClear=FALSE){
        static $data=array();
        if($isClear)
            $data=array();
        foreach($info as $k=>$v){
            if($v['parent_id']==$parent_id){
                $v['level']=$level;
                $data[]=$v;
                $this->_getCatTree($info,$v['id'],$level+1);
            }
        }
        return $data;
    }
    /***********************************获取一个服务分类的子服务分类****************************************/
    public function getCatChild($cat_id){
        $info=Db::table('shop_goods_cat')->select();
        return $this->_getCatChild($info,$cat_id,TRUE);
    }
    private function _getCatChild($info,$parent_id,$isClear=FALSE){
        static $data=array();
        if($isClear)
            $data=array();
        foreach($info as $k=>$v){
            if($v['parent_id']==$parent_id){
                $data[]=$v['id'];
                $this->_getCatChild($info,$v['id']);
            }
        }
        return $data;
    }
}
